package com.yqg.protection.common.config;

import com.yqg.protection.common.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${files.upload.path}")
    private String filePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/file/download/**") // 添加访问路径
//                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                // file: 当前项目根目录, 映射到真实的路径下的 uploadPath(变量)目录下
                .addResourceLocations("file:" + "/");
//                .resourceChain(false);
    }

    @Bean
    public TokenInterceptor tokenInterceptor(){
        return new TokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns=new String[]{"/**/export", "/file/**", "/files/**", "/video/**", "/qrCode/generator", "/inspect-addr-code/newAddrData", "/aiot/**"};
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(tokenInterceptor());
        interceptorRegistration.addPathPatterns("/**");
        interceptorRegistration.excludePathPatterns(patterns)
                .excludePathPatterns( // 不拦截路径
                        "/user/login",
                        "/user/register",
                        "/**/*.html",            //html静态资源
                        "/**/*.js",              //js静态资源
                        "/**/*.css",             //css静态资源
                        "/**/*.woff",
                        "/files/**")
                .excludePathPatterns("/swagger-resources/**", "/swagger-ui/**", "/v3/**", "/error");
    }
}
